package com.abu.ucenter.controller;


import com.abu.exceptionhandler.EduException;
import com.abu.ucenter.pojo.Member;
import com.abu.ucenter.pojo.RegisterVo;
import com.abu.ucenter.pojo.UserInfo;
import com.abu.ucenter.service.MemberService;
import com.abu.utils.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-21
 */
@RestController
@RequestMapping("/educenter/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private PasswordEncoder passwordEncoder;

    private static final String EXCHANGE = "UCENTER.REGISTER";
    private static final String ROUTING = "UCENTER.REGISTER";

    @GetMapping("send/{phone}")
    public Resp sendMsm(@PathVariable("phone") String phone) {
        this.amqpTemplate.convertAndSend(EXCHANGE, ROUTING, phone);
        return Resp.ok();
    }

    @GetMapping("queryCountByDate/{date}")
    public Integer queryCountByDate(@PathVariable("date") String date) {
        return this.memberService.queryCount(date);
    }

    /**
     * 用户注册
     *
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public Resp register(@RequestBody RegisterVo registerVo) {
        Boolean flag = this.memberService.register(registerVo);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().message("注册失败，该手机号已经存在");
    }

    @PostMapping("queryMember")
    public Member queryMemberByPhoneAndPassword(@RequestBody UserInfo userInfo) {
//        1.根据手机号查询用户是否存在
        Member member = this.memberService.getOne(new QueryWrapper<Member>().eq("mobile", userInfo.getMobile()));
        if (member == null) {
            throw new EduException(20001, "用户不存在");
        }
//        2.比较密码是否一致
        boolean flag = this.passwordEncoder.matches(userInfo.getPassword(), member.getPassword());
        if (!flag) {
            return null;
        }
        return member;
    }

    @GetMapping("{id}")
    public Member queryMemberById(@PathVariable("id") String id) {
        Member member = this.memberService.getById(id);
        if (member == null) {
            throw new EduException(20001, "用户不存在");
        }
        return member;
    }

    @GetMapping("/openId/{openId}")
    public Member queryMemberByOpenId(@PathVariable("openId") String openId) {
        return this.memberService.getOne(new QueryWrapper<Member>().eq("openid", openId));
    }

    @PostMapping("save")
    public Member save(@RequestBody Member member) {
        boolean flag = this.memberService.save(member);
        if (!flag) {
            return null;
        }
        return member;
    }
}

